import Vue from 'vue'
function timeFormatter(cell, needHour, needSec) {
  let date = new Date(cell)
  let year = date.getFullYear()
  let month = date.getMonth() + 1
  month = month >= 10 ? month : '0' + month
  let d1 = date.getDate()
  d1 = d1 >= 10 ? d1 : '0' + d1
  let time_str = year + '-' + month + '-' + d1
  if (needHour) {
    let h = date.getHours()
    h = h >= 10 ? h : '0' + h
    let m = date.getMinutes()
    m = m >= 10 ? m : '0' + m
    time_str += ' ' + h + ':' + m
  }
  if (needSec) {
    let s = date.getSeconds()
    s = s >= 10 ? s : '0' + s
    time_str += ':' + s
  }
  return cell ? time_str : '--'
}
function formatHours(date3) {
  date3 = date3 * 3600 * 1000
  //计算出相差天数
  let days = Math.floor(date3 / (24 * 3600 * 1000))

  //计算出小时数
  let leave1 = date3 % (24 * 3600 * 1000) //计算天数后剩余的毫秒数
  let hours = Math.floor(leave1 / (3600 * 1000))

  //计算相差分钟数
  let leave2 = leave1 % (3600 * 1000) //计算小时数后剩余的毫秒数
  let minutes = Math.floor(leave2 / (60 * 1000))

  return (days ? days + '天' : '') + (hours ? hours + '小时' : '') + minutes + '分钟'
}
Vue.prototype.$format = timeFormatter
Vue.prototype.$formatHours = formatHours
